<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>报价单功能测试</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 800px;
            margin: 0 auto;
            padding: 20px;
        }
        .test-section {
            margin-bottom: 30px;
            padding: 20px;
            border: 1px solid #ddd;
            border-radius: 5px;
        }
        .test-section h3 {
            color: #333;
            margin-bottom: 15px;
        }
        button {
            padding: 8px 16px;
            margin: 5px;
            border: none;
            border-radius: 3px;
            cursor: pointer;
            background-color: #409eff;
            color: white;
        }
        button:hover {
            background-color: #337ab7;
        }
        #result {
            margin-top: 20px;
            padding: 15px;
            background-color: #f5f5f5;
            border-radius: 5px;
            white-space: pre-wrap;
        }
    </style>
</head>
<body>
    <h1>报价单功能测试</h1>
    
    <div class="test-section">
        <h3>1. 后端连接测试</h3>
        <button onclick="testBackendConnection()">测试后端连接</button>
        <button onclick="testHealthCheck()">健康检查</button>
    </div>
    
    <div class="test-section">
        <h3>2. 报价单基础功能测试</h3>
        <button onclick="testGetQuotationList()">获取报价单列表</button>
        <button onclick="testGetCustomerOptions()">获取客户选项</button>
        <button onclick="testGetEmployeeOptions()">获取员工选项</button>
    </div>
    
    <div class="test-section">
        <h3>3. 报价单数据操作测试</h3>
        <button onclick="testCreateQuotation()">创建报价单</button>
        <button onclick="testSaveDraft()">保存草稿</button>
        <button onclick="testUpdateQuotation()">更新报价单</button>
    </div>
    
    <div id="result"></div>

    <script>
        const API_BASE_URL = 'http://localhost:8080/api';
        
        function showResult(title, data) {
            const resultDiv = document.getElementById('result');
            const timestamp = new Date().toLocaleString();
            resultDiv.textContent = `[${timestamp}] ${title}\n${JSON.stringify(data, null, 2)}`;
        }
        
        async function apiRequest(url, options = {}) {
            try {
                const response = await fetch(`${API_BASE_URL}${url}`, {
                    headers: {
                        'Content-Type': 'application/json',
                        ...options.headers
                    },
                    ...options
                });
                
                if (!response.ok) {
                    throw new Error(`HTTP error! status: ${response.status}`);
                }
                
                return await response.json();
            } catch (error) {
                return { error: error.message };
            }
        }
        
        async function testBackendConnection() {
            const result = await apiRequest('/hello');
            showResult('后端连接测试', result);
        }
        
        async function testHealthCheck() {
            const result = await apiRequest('/hello/status');
            showResult('健康检查', result);
        }
        
        async function testGetQuotationList() {
            const result = await apiRequest('/quotations?current=1&size=10');
            showResult('获取报价单列表', result);
        }
        
        async function testGetCustomerOptions() {
            const result = await apiRequest('/quotations/customer-options');
            showResult('获取客户选项', result);
        }
        
        async function testGetEmployeeOptions() {
            const result = await apiRequest('/quotations/employee-options');
            showResult('获取员工选项', result);
        }
        
        async function testCreateQuotation() {
            const quotationData = {
                customerCode: 'TEST001',
                customerName: '测试客户',
                salesperson: '张三',
                orderDate: '2024-01-15',
                isTaxIncluded: false,
                paymentMethod: '月结30天',
                remark: '测试报价单',
                items: [
                    {
                        itemNo: 1,
                        productName: '测试PCB',
                        materialCode: 'PCB001',
                        layers: '4',
                        thickness: '1.6',
                        surfaceTreatment: '无铅喷锡',
                        solderMask: '绿油',
                        character: '白字',
                        boardMaterial: 'FR-4',
                        quantity: 100,
                        unitPrice: 2.5,
                        totalAmount: 250,
                        deliveryDate: '2024-01-25'
                    }
                ]
            };
            
            const result = await apiRequest('/quotations', {
                method: 'POST',
                body: JSON.stringify(quotationData)
            });
            showResult('创建报价单', result);
        }
        
        async function testSaveDraft() {
            const draftData = {
                customerCode: 'DRAFT001',
                customerName: '草稿客户',
                salesperson: '李四',
                orderDate: '2024-01-15',
                status: '草稿',
                items: []
            };
            
            const result = await apiRequest('/quotations/draft', {
                method: 'POST',
                body: JSON.stringify(draftData)
            });
            showResult('保存草稿', result);
        }
        
        async function testUpdateQuotation() {
            // 先获取一个报价单ID，这里假设有ID为1的报价单
            const updateData = {
                id: 1,
                customerCode: 'UPDATE001',
                customerName: '更新客户',
                salesperson: '王五',
                remark: '更新测试'
            };
            
            const result = await apiRequest('/quotations/1', {
                method: 'PUT',
                body: JSON.stringify(updateData)
            });
            showResult('更新报价单', result);
        }
    </script>
</body>
</html> 